/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "taskhub.h"
#include "task.h"
#include "log.h"

namespace Wishmaster
{
	Taskhub::Taskhub() : m_mutex(QMutex::Recursive)
	{
		m_activeQueueLength = 20;
	}

	Taskhub::~Taskhub()
	{
	}

	void Taskhub::queue(const TaskPtr& t, bool first)
	{
		QMutexLocker locker(&m_mutex);
		if(m_active.length() == 0)
		{
			m_done = 0;
			m_total = 0;
		}
		m_total += 100;
		if(m_active.length() < m_activeQueueLength)
		{
			m_active.append(t);
			t->start();
		}
		else
		{
			if(first)
				m_pending.prepend(t);
			else
				m_pending.append(t);

		}
		connect(t.data(), SIGNAL(done(int)), this, SLOT(slot_taskDone(int)));
		connect(t.data(), SIGNAL(progress(int, int, int)), this, SLOT(slot_taskProgress(int, int, int)));
		emit taskProgress(t->id(), 0);
	}

	void Taskhub::queueTaskFirst(const TaskPtr& t)
	{
		queue(t, true);
	}

	void Taskhub::queueTask(const TaskPtr& t)
	{
		queue(t, false);
	}

	void Taskhub::taskPercentage(int id, int percentage)
	{
		QMutexLocker locker(&m_mutex);
		foreach(const TaskPtr& t, m_tasks)
		{
			if(t->id() == id)
			{
				if(percentage >= 100)
					emit taskDone(t);
				t->setPercentage(percentage);
			}
		}
		update();
		emit taskProgress(id, percentage);
	}

	void Taskhub::cancelTask(int id)
	{
		QMutexLocker locker(&m_mutex);
		int i = 0;
		foreach(const TaskPtr& t, m_active)
		{
			if(t->id() == id)
			{
				t->cancel();
				m_total -= 100;
				m_active.removeAt(i);
				update();
				return;
			}
			i++;
		}
		i = 0;
		foreach(const TaskPtr& t, m_pending)
		{
			if(t->id() == id)
			{
				t->cancel();
				m_total -= 100;
				m_pending.removeAt(i);
				update();
				return;
			}
			i++;
		}
	}

	void Taskhub::slot_taskDone(int id)
	{
		QMutexLocker locker(&m_mutex);
		for(QList<TaskPtr>::iterator it = m_active.begin(); it != m_active.end(); ++it)
		{
			if((*it)->id() == id)
			{
				emit taskDone(*it);
				m_active.erase(it);
				m_done += 100;
				break;
			}
		}
		while(m_pending.size() > 0 && m_active.size() < m_activeQueueLength)
		{
			TaskPtr task = m_pending.front();
			m_pending.pop_front();
			m_total -= 100;
			queueTask(task);
		}
		update();
	}

	void Taskhub::slot_taskProgress(int id, int done, int total)
	{
		update();
		emit taskProgress(id, (double)done/total * 100.);
	}

	void Taskhub::update()
	{
		QMutexLocker locker(&m_mutex);
		int sum = m_done;
		foreach(const TaskPtr& task, m_active)
		{
			sum += task->percentage();
		}
		emit totalProgress((double)sum / m_total * 100.);
	}

	int Taskhub::taskCount()
	{
		return m_active.size() + m_pending.size();
	}

	TaskPtr Taskhub::taskByIndex(int index)
	{
		QMutexLocker locker(&m_mutex);
		if(index >= m_active.size())
		{
			index -= m_active.size();
			if(index < m_pending.size())
				return m_pending.at(index);
		}
		else
			return m_active.at(index);
		return TaskPtr();
	}

	TaskPtr Taskhub::taskById(int id)
	{
		QMutexLocker locker(&m_mutex);
		foreach(const TaskPtr& task, m_active)
		{
			if(task->id() == id)
				return TaskPtr(task);
		}
		return TaskPtr();
	}
}

